const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");

module.exports = {
  mode: "production",
  devtool: false,
  entry: {
    index: {
      import: "./src/index.js",
      dependOn: "shared1", // 共享那个模块
    },
    main: {
      import: "./src/main.js",
      dependOn: "shared1",
    },
    shared1: ["axios"],
  },
  output: {
    filename: "[name]_[chunkhash:5].js",
    path: path.resolve(__dirname, "dist"),
    clean: true,
  },
  target: "web",
  // optimization: {
  //   splitChunks: {
  //     chunks: "all",
  //   },
  // },
  devServer: {
    static: ["public", "content"], // 静态资源目录，默认为public
    liveReload: false, // 编译失败后是否重新刷新整个页面
    host: "0.0.0.0",
    port: "3000",
    open: true,
    compress: true, // 打包的文件是否压缩
    proxy: [
      {
        context: ["/api", "/auth"], // 需要代理的路径
        target: "http://localhost:9000", // 目标服务器
        changeOrigin: true, // 修改请求头中的 host
        pathRewrite: {
          "^/api": "", // 重写路径（可选）
        },
      },
    ],
    historyApiFallback: true, // 页面404时是否重定向到首页
  },
  resolve: {
    extensions: [".js", ".json", ".jsx", ".vue", ".ts"],
  },
  module: {
    rules: [
      {
        test: /\.m?jsx?$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
      {
        test: /\.ts$/,
        use: ["babel-loader"],
        // use: "babel-loader",
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
    }),
  ],
};
